<template>
  <div class="home">
    <el-container>
      <!-- 页面头部 -->
      <el-header>
        <div class="left">
            <img src="../assets/heima.png" alt="">
            <span>杭州前端12期后台管理系统</span>
        </div>
        <el-button @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主体 -->
      <el-container>
        <!-- 主体左侧导航 -->
        <el-aside :width="collapse?'64px':'200px'">
          <!-- 折叠导航 -->
          <!-- :能将字符串变成数据类型 -->
          <div class="toggleMenu" @click="collapse=!collapse">|||</div>
          <el-menu
            unique-opened
            router
            :collapse="collapse"
            :collapse-transition="false"
            :default-active="activeDefault"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff">
            <el-submenu v-for="item in menus" :key="item.id" :index="item.id+''">
              <!-- 一级导航的模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsList[item.id]"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级模板 -->
              <el-menu-item v-for="item2 in item.children" :key="item2.id" :index="'/'+item2.path">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{item2.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体右侧内容 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menus: [],
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      collapse: false,
      activeDefault: ''
    }
  },

  created () {
    this.getMenus()
    this.activeDefault = this.$route.path
  },

  methods: {
    // 退出
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 定义获取菜单数据
    async getMenus () {
      const { data: res } = await this.$axios.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menus = res.data
    }
  }
}
</script>

<style scoped lang='less'>
.home, .el-container {
  height:100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  align-items: center;
  .left {
      display: flex;
      align-items: center;
      span {
          font-size: 25px;
          color: #fff;
          margin-left: 15px;
      }
  }
}
.el-aside {
  background: #333744;
  .iconfont{
    margin-right: 8px;
  }
  .toggleMenu{
    width: 100%;
    height: 40px;
    background: #666;
    line-height: 40px;
    text-align: center;
    letter-spacing: 5px;
    cursor: pointer;
    user-select:none
  }
}
.el-main {
  background: #eaedf1;
}
.el-menu {
  border-right: none;
}
</style>
